package com.xcalendar.service.rest.converter;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.google.appengine.api.datastore.KeyFactory;
import com.xcalendar.database.table.Event;
import com.xcalendar.database.table.Location;
import com.xcalendar.database.table.User;

@XmlRootElement(name = "user")
public class UserConverter {
	private User entity = null;
	private String errorString;
	public UserConverter() {
		entity = new User();
	}

	public UserConverter(User entity) {
		this.entity = entity;
	}

	@XmlElement
	public String getKey() {
		if (entity.getKey() != null) {
			return KeyFactory.keyToString(entity.getKey());
		}else {
			return null;
		}
		
	}
	
	@XmlElement
	public String getUserName() {
		return entity.getUserName();
	}

	@XmlElement
	public String getPassword() {
		return entity.getPassword();
	}

	@XmlElement
	public List<EventConverter> getEvents() {
		if (entity.getEvents() != null) {
			List<EventConverter> ecs = new ArrayList<EventConverter>();
			for (int i = 0; i < entity.getEvents().size(); i++) {
				EventConverter eventConverter = new EventConverter(entity.getEvents().get(i));
				ecs.add(eventConverter);
			}
			return ecs;
		}else {
			return null;
		}
		
	}
	@XmlElement
	public List<LocationConverter> getLocations() {
		if (entity.getLocations() != null) {
			List<LocationConverter> ecs = new ArrayList<LocationConverter>();
			for (int i = 0; i < entity.getLocations().size(); i++) {
				LocationConverter eventConverter = new LocationConverter(entity.getLocations().get(i));
				ecs.add(eventConverter);
			}
			return ecs;
		}else {
			return null;
		}
		
	}
	@XmlElement
	public String getError() {
		return errorString;
	}
	public void setError(String error) {
		errorString = error;
	}
	public User getUser() {
		return entity;
	}

	public void setKey(String key) {
		entity.setKey(KeyFactory.stringToKey(key));
	}
	
	public void setUserName(String userName) {
		entity.setUserName(userName);
	}

	public void setPassword(String password) {
		entity.setPassword(password);
	}

	public void setEvents(List<Event> events) {
		entity.setEvents(events);
	}
	
	public void setLocations(List<Location> locations) {
		entity.setLocations(locations);
	}
}
